package com.xingxue.logistics.controller.sys;

import com.xingxue.logistics.dao.domain.SysMenu;
import com.xingxue.logistics.dao.domain.SysRole;
import com.xingxue.logistics.dao.domain.SysUser;
import com.xingxue.logistics.service.MenuService;
import com.xingxue.logistics.service.RoleService;
import com.xingxue.logistics.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author zgx at 2018/10/11 0011 上午 11:45
 */

@RestController
@RequestMapping("sys/roleAccredit")
@Api(description = "角色赋权相关控制器")
public class RoleAccreditController {
    @Autowired
    RoleService roleService;
    @Autowired
    MenuService menuService;
    @Autowired
    UserService userService;


    @ApiOperation("加载角色列表")
    @GetMapping("loadRole")
    public ResponseEntity loadRole(){
        List<SysRole> sysRoleList=roleService.loadAll();
        return ResponseEntity.ok(sysRoleList);
    }

    @ApiOperation("加载用户列表")
    @GetMapping("loadUser")
    public ResponseEntity loadUser(){
        List<SysUser> sysUserList=userService.loadAll();
        return ResponseEntity.ok(sysUserList);
    }

    @ApiOperation("加载菜单列表")
    @GetMapping("loadMenu")
    public ResponseEntity loadMenu(){
        List<SysMenu> sysMenuList=menuService.loadAll();
        return ResponseEntity.ok(sysMenuList);
    }



    @ApiOperation("根据角色加载菜单")
    @GetMapping("role_menu/{roleId}")
    public ResponseEntity loadRoleMenu(@PathVariable("roleId") @ApiParam("角色ID") Integer roleId){
        List<SysMenu> node = menuService.loadMenuByRoleId(roleId);
        return ResponseEntity.ok(node);
    }
    @ApiOperation("根据角色加载用户")
    @GetMapping("role_user/{roleId}")
    public ResponseEntity loadRoleUser(@PathVariable("roleId") @ApiParam("角色ID") Integer roleId){
        List<SysUser> node = userService.loadUserByRoleId(roleId);
        return ResponseEntity.ok(node);
    }

    @ApiOperation("更新菜单权限")
    @PutMapping("saveMenu/{roleIdData}/{idsArr}")
    public String saveMenu(@PathVariable("roleIdData") @ApiParam(value = "角色ID",required = true) Integer roleId,@PathVariable("idsArr") Integer[] idsArr){
        menuService.updateMenuByRoleId(roleId,idsArr);
        return "ok";
    }

    @ApiOperation("更新用户权限")
    @PutMapping("saveUser/{roleIdData}/{userIdsArr}")
    public String saveUser(@PathVariable("roleIdData") @ApiParam(value = "角色ID",required = true) Integer roleId,@PathVariable("userIdsArr") Integer[] userIdsArr){
        userService.updateUserByRoleId(roleId,userIdsArr);
        return "ok";
    }


}
